import {
  Controller,
  Get,
  UseInterceptors,
} from "@nestjs/common";

// 获取 控制器级别的 拦截器
import { Logger3Interceptor } from './interceptors/logger3.interceptor'
import { Logger4Interceptor } from './interceptors/logger4.interceptor'

// 获取 路由处理函数级别的 拦截器 
import { Logger1Interceptor } from './interceptors/logger1.interceptor'
import { Logger2Interceptor } from './interceptors/logger2.interceptor'
import { TransformInterceptor } from "./interceptors/transform.interceptor";
import { ExcludeNullInterceptor } from './interceptors/excludeNull.interceptor'
import { ErrorsInterceptor } from "./interceptors/error.interceptor";
import { CatchInterceptor } from './interceptors/catch.interceptor'
import { TimeoutInterceptor } from './interceptors/timeout.interceptor'

@Controller('pay')
@UseInterceptors(Logger3Interceptor)
@UseInterceptors(Logger4Interceptor)
export class PayController {
  @Get()
  @UseInterceptors(Logger1Interceptor)
  @UseInterceptors(Logger2Interceptor)
  async index() {
    console.log('pay 路由处理函数')
    return 'pay--1'
  }

  @Get('data')
  @UseInterceptors(TransformInterceptor)
  async data() {
    console.log('data...')
    return 'data'
  }

  @Get('null')
  @UseInterceptors(ExcludeNullInterceptor, TransformInterceptor)
  async nullHandle() {
    console.log('nullHandle...')
    return null
  }

  @Get('error')
  @UseInterceptors(ErrorsInterceptor)
  async errorHandle() {
    console.log('exception...')
    throw new Error('自定义错误')
  }

  @Get('user')
  @UseInterceptors(CatchInterceptor)
  async user() {
    console.log('user... 执行了路由处理函数')
    return {
      name: 'user',
      age: 18
    }
  }

  @Get('timeout')
  // @UseInterceptors(TimeoutInterceptor)
  async timeout() {
    console.log('TimeoutInterceptor 执行路由处理函数')
    await new Promise<void>(resolve => {
      setTimeout(() => {
        resolve()
      }, 3000)
    })
    return 'timeout'
  }
}
